package com.tbit.uqbike.service.base.impl;

import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.Account;
import com.tbit.uqbike.object.pojo.AlarmDayCount;
import com.tbit.uqbike.object.pojo.RideLog;
import com.tbit.uqbike.object.pojo.TerAlarm;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.NameObject;
import com.tbit.uqbike.object.view.sugar.CoreMoney;
import com.tbit.uqbike.service.base.SugarService;
import com.tbit.uqbike.service.rpcservice.PurviewService;
import com.tbit.uqbike.service.rpcservice.StatService;
import com.tbit.uqbike.webmanager.dao.core.AccountDao;
import com.tbit.uqbike.webmanager.dao.core.SugarDao;
import com.tbit.uqbike.webmanager.dao.log.PowerChangeLogDao;
import com.tbit.uqbike.webmanager.dao.stat.AccountSumStatDao;
import com.tbit.utils.DateTimeUtil;
import com.tbit.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

import static java.util.stream.Collectors.toList;

/**
 * @author MyWin E-mail:335918956@qq.com
 * @version 1.0
 * @createTime 2020-01-06 19:10
 */
@Slf4j
@Service
public class SugarServiceImpl implements SugarService {
    @Autowired
    private SugarDao sugarDao;
    @Autowired
    private PurviewService purviewService;
    @Autowired
    private AccountDao accountDao;
    @Autowired
    private StatService statService;
    @Autowired
    private PowerChangeLogDao powerChangeLogDao;

    @Override
    public Integer getTotalUserCnt(LoginInfo li, List<Integer> accountIds) {
        Account account = accountDao.getByAccountId(li.id);
        if (null == account) {
            throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
        }
        List<Integer> ids = purviewService.filterAgent(account, accountIds);
        if (ids.isEmpty()) {
            return 0;
        }
        return sugarDao.getTotalUserCnt(ids);
    }

    @Override
    public Integer getTodayUserCnt(LoginInfo li, List<Integer> accountIds) {
        Account account = accountDao.getByAccountId(li.id);
        if (null == account) {
            throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
        }
        List<Integer> ids = purviewService.filterAgent(account, accountIds);
        if (ids.isEmpty()) {
            return 0;
        }
        return sugarDao.getTodayUserCnt(ids);
    }

    @Override
    public List<RideLog> getNearRide(LoginInfo li, List<Integer> accountIds) {
        Account account = accountDao.getByAccountId(li.id);
        if (null == account) {
            throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
        }
        List<Integer> ids = purviewService.filterAgent(account, accountIds);
        if (ids.isEmpty()) {
            return new LinkedList<>();
        }
        return sugarDao.getNearRide(ids);
    }

    @Override
    public CoreMoney getFeeCore(LoginInfo li, List<Integer> accountIds) {
        Account account = accountDao.getByAccountId(li.id);
        if (null == account) {
            throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
        }
        List<Integer> ids = purviewService.filterAgent(account, accountIds);
        if (ids.isEmpty()) {
            return new CoreMoney();
        }
        // 总数
        Long start = DateUtil.getDate(li.zone);
        Double currDayMoney = statService.statTotalMoney(li, ids, start, start + DateUtil.DAY_MS);
        Double yesterDayMoney = statService.statTotalMoney(li, ids, start - DateUtil.DAY_MS, start);
        long weekStart = DateUtil.getWeek(li.zone);
        Double currWeekMoney = statService.statTotalMoney(li, ids, weekStart, weekStart + DateUtil.DAY_MS * 7L);
        Double lastWeekMoney = statService.statTotalMoney(li, ids, weekStart - DateUtil.DAY_MS * 7L, weekStart);
        CoreMoney cm = new CoreMoney();
        cm.currDayMoney = currDayMoney;
        cm.currWeekMoney = currWeekMoney;
        cm.lastWeekMoney = lastWeekMoney;
        cm.yesterDayMoney = yesterDayMoney;
        return cm;
    }

    @Override
    public Double getSpeFee(LoginInfo li, List<Integer> accountIds, String feeName) {
        Account account = accountDao.getByAccountId(li.id);
        if (null == account) {
            throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
        }
        List<Integer> ids = purviewService.filterAgent(account, accountIds);
        if (ids.isEmpty()) {
            return 0.0;
        }
        if (Objects.equals(feeName, "totalMoney")) {
            return statService.statTotalMoney(li, ids, null, null);
        } else if (Objects.equals(feeName, "yesterDayMoney")) {
            Long start = DateUtil.getDate(li.zone);
            return statService.statTotalMoney(li, ids, start - DateUtil.DAY_MS, start);
        } else if (Objects.equals(feeName, "yesterDayMoneyCompare")) {
            // 环比
            long start = DateUtil.getDate(li.zone);
            Double one = statService.statTotalMoney(li, ids, start - DateUtil.DAY_MS, start);
            Double two = statService.statTotalMoney(li, ids, start - DateUtil.DAY_MS * 2, start - DateUtil.DAY_MS * 2);
            if (two == 0) {
                return 0.0;
            }
            return (one - two) * 100 / two;
        } else if (Objects.equals(feeName, "yearYesterDayMoneyCompare")) {
            // 同比
            Long start = DateUtil.getDate(li.zone);
            Long lastStart = DateUtil.getLastYearDate(li.zone);
            Double one = statService.statTotalMoney(li, ids, start - DateUtil.DAY_MS, start);
            Double two = statService.statTotalMoney(li, ids, lastStart - DateUtil.DAY_MS, lastStart);
            if (two == 0) {
                return 0.0;
            }
            return (one - two) * 100 / two;
        }
        return 0.0;
    }

    @Override
    public Map<String, Double> statTotalMoney(LoginInfo li, List<Integer> accountIds) {
        Account account = accountDao.getByAccountId(li.id);
        if (null == account) {
            throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
        }
        Map<String, Double> map = new HashMap<>();
        Map<String, Double> sortMap = new HashMap<>();
        List<Integer> ids = purviewService.filterAgent(account, accountIds);
        if (ids.isEmpty()) {
            return map;
        }
        for (Integer id : ids) {
            Account acc = accountDao.getByAccountId(id);
            if (acc == null) {
                continue;
            }
            List<Integer> idList = new LinkedList<>();
            idList.add(id);
            /**统计今天之前的总收入*/
            Double d = statService.statTotalMoney(li, idList, null, null);
            map.put(acc.getName(), d);
        }
        /**对值排序获取前15*/
        LinkedList<Map.Entry<String, Double>> list = new LinkedList<>(map.entrySet());
        //获取收入前十五的运营区域
        list.sort((o1, o2) -> o2.getValue().compareTo(o1.getValue()));
        list.forEach(entry -> {
            if (sortMap.size() <= 10) {
                sortMap.put(entry.getKey(), entry.getValue());
            }
        });
        return sortMap;
    }

    @Override
    public List<Integer> filterAgent(LoginInfo li, List<Integer> accountIds) {
        Account account = accountDao.getByAccountId(li.id);
        if (null == account) {
            throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
        }
        return purviewService.filterAgent(account, accountIds);
    }

    @Override
    public Map<String, Double> statTotalChangePower(LoginInfo li, List<Integer> accountIds) {
        Date startTime = DateTimeUtil.getYesterdayHHMMSS(-1);
        Date endTime = DateTimeUtil.getYesterdayHHMMSS(0);
        Account account = accountDao.getByAccountId(li.id);
        if (null == account) {
            throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
        }
        Map<String, Double> map = new HashMap<>();
        List<Integer> ids = purviewService.filterAgent(account, accountIds);
        if (ids.isEmpty()) {
            return map;
        }
        for (Integer id : ids) {
            Account acc = accountDao.getByAccountId(id);
            if (acc == null) {
                continue;
            }
            int i = powerChangeLogDao.countPowerChangeLogTimes(id, startTime, endTime);
            map.put(acc.getName(), (double) i);
        }
        return map;
    }

    @Override
    public Map<Integer, Integer> statAlarmCount(LoginInfo li, List<Integer> accountIds) {
        Account account = accountDao.getByAccountId(li.id);
        if (null == account) {
            throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
        }
        Map<Integer, Integer> map = new HashMap<>();
        List<Integer> ids = purviewService.filterAgent(account, accountIds);
        if (ids.isEmpty()) {
            return new HashMap<>();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("accountIds", ids);
        List<String> mnos = accountDao.getMnosByIds(params);
        if (mnos.isEmpty()) {
            return new HashMap<>();
        }
        params.put("mnos", mnos);
       /* List<NameCount> ncs = terAlarmDao.statAlarmCount(params);
        for (NameCount nc : ncs) {
            map.put(nc.getC1(), nc.getC2());
        }*/
        return map;
    }

    @Override
    public List<AlarmDayCount> statAlarmDetail(LoginInfo li, List<Integer> accountIds) {
        /*Account account = accountDao.getByAccountId(li.id);
        if (null == account) {
            throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
        }
        List<Integer> ids = purviewService.filterAgent(account, accountIds);
        if (ids.isEmpty()) {
            return new LinkedList<>();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("accountIds", ids);
        List<String> mnos = accountDao.getMnosByIds(params);
        if (mnos.isEmpty()) {
            return new LinkedList<>();
        }
        params.put("mnos", mnos);
        return terAlarmDao.statAlarmDetail(params);*/
        return new LinkedList<>();
    }

    @Override
    public List<TerAlarm> queryAlarmInfo(LoginInfo li, List<Integer> accountIds) {
        /*Account account = accountDao.getByAccountId(li.id);
        if (null == account) {
            throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
        }
        List<Integer> ids = purviewService.filterAgent(account, accountIds);
        if (ids.isEmpty()) {
            return new LinkedList<>();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("accountIds", ids);
        List<String> mnos = accountDao.getMnosByIds(params);
        if (mnos.isEmpty()) {
            return new LinkedList<>();
        }
        params.put("mnos", mnos);
        return terAlarmDao.queryAlarmInfo(params);*/
        return new ArrayList<>();
    }

    @Resource
    private AccountSumStatDao accountSumStatDao;

    @Override
    public List<NameObject> statRecharge(LoginInfo li, List<Integer> accountIds, Integer brandId) {
        return accountSumStatDao.orderCondition(accountIds, brandId)
                .stream().map(x -> new NameObject(x.getName(), x.getValue()))
                .collect(toList());
    }

}
